package easy;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-05-15 22:11
 */
public class SearchNumber {
    public static int searchInsert(int[] nums, int target) {
        if (nums[0] >= target) {
            return 0;
        }

        if (nums[nums.length - 1] == target) {
            return nums.length - 1;
        }

        if (nums[nums.length - 1] < target) {
            return nums.length;
        }

        int start = 0;
        int end = nums.length - 1;
        while (start < end - 1) {
            int half = (start + end) / 2;
            int temp = nums[half];
            if (temp == target) {
                return half;
            } else if (temp > target) {
                end = half;
            } else {
                start = half;
            }
        }

        return start + 1;
    }

    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        int target = 2;
        int i = searchInsert(nums, target);
        System.out.println(i);
    }
}
